package msgActivity;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class Adapter extends BaseAdapter{
	
	List<Data> mList;
	Context mContext;
	
	public static final int ITEM_VIEW_COUNT = 3;
	public static final int ITEM_VIEW_TYPE_SEND = 0;
	public static final int ITEM_VIEW_TYPE_RECIEVE = 1;
	public static final int ITEM_VIEW_TYPE_DATE = 2;

	
	public Adapter(Context context, List<Data> list) {
		mContext = context;
		mList = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Data data = mList.get(position);
		
		if(data.name.equals("date")){
			ItemDate view;

			if (convertView == null) {
				view = new ItemDate(mContext);
			} else {
				view = (ItemDate)convertView;
			}
			view.setData(data);
			return view;
		}
		
		if(data.isSend == true) {
			ItemSend view;
			
			if(convertView == null) {
			view = new ItemSend(mContext);
			} else {
				view = (ItemSend)convertView;
			}
			view.setData(data);
			return view;
		} else {
			ItemReceive view;
			if(convertView == null) {
			view = new ItemReceive(mContext);
			} else {
				view = (ItemReceive)convertView;
			}
			view.setData(data);
			return view;
		}
		
	}
	
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return ITEM_VIEW_COUNT;
	}
	
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		Data data = mList.get(position);
		
		if(data.name.equals("date"))
			return ITEM_VIEW_TYPE_DATE;
		
		if (data.isSend == true) {
			return ITEM_VIEW_TYPE_SEND;
		} else {
			return ITEM_VIEW_TYPE_RECIEVE;
		}
		
	}
	
	public void add(Data myData) {
		mList.add(myData);
		notifyDataSetChanged();
	}

}
